package com.zf.zfed.audit.ui

import android.content.Context
import android.util.AttributeSet
import android.view.Gravity
import android.widget.LinearLayout
import android.widget.ImageView
import android.widget.TextView
import androidx.core.content.ContextCompat
import com.zf.zfed.audit.R

/**
 * 密码操作按钮组件
 */
class PasswordActionButton @JvmOverloads constructor(
    context: Context,
    attrs: AttributeSet? = null,
    defStyleAttr: Int = 0
) : LinearLayout(context, attrs, defStyleAttr) {
    
    private var actionIcon: ImageView
    private var actionText: TextView
    
    init {
        // 使用水平线性布局
        orientation = HORIZONTAL
        gravity = Gravity.CENTER
        
        isClickable = true
        isFocusable = true
        
        // 设置背景
        background = ContextCompat.getDrawable(context, R.drawable.password_action_button_background)
        
        // 设置内边距
        val padding = context.dpFromDimen(R.dimen.spacing_8)
        setPadding(padding, padding, padding, padding)
        
        // 图标
        actionIcon = ImageView(context)
        val iconSize = context.dpFromDimen(R.dimen.size_16)
        val iconLP = LayoutParams(iconSize, iconSize)
        iconLP.rightMargin = context.dpFromDimen(R.dimen.spacing_4) // 图标右边距
        addView(actionIcon, iconLP)
        
        // 文字
        actionText = TextView(context)
        actionText.setTextColor(ContextCompat.getColor(context, R.color.text_white))
        actionText.textSize = context.spFromDimen(R.dimen.font_16)
        actionText.setTypeface(null, android.graphics.Typeface.BOLD)
        actionText.maxLines = 1
        actionText.ellipsize = android.text.TextUtils.TruncateAt.END
        addView(actionText)
    }
    
    /**
     * 设置操作图标
     */
    fun setActionIcon(iconRes: Int) {
        actionIcon.setImageResource(iconRes)
        actionIcon.setColorFilter(ContextCompat.getColor(context, R.color.text_primary))
    }
    
    /**
     * 设置操作文字
     */
    fun setActionText(text: String) {
        actionText.text = text
    }
    
    override fun setEnabled(enabled: Boolean) {
        super.setEnabled(enabled)
        alpha = if (enabled) 1.0f else 0.5f
    }
}

